JavaScript syntax
part 28/43 Β· 161.3 KB total
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
b.
console.log(c ? t : f); // When c is true, there is no reason to
evaluate f.
In early versions of JavaScript and JScript, the binary logical
operators returned a Boolean value (like most C-derived programming
languages). However, all contemporary implementations return one of
their operands instead:
console.log(a || b); // if a is true, return a, otherwise return b
console.log(a && b); // if a is false, return a, otherwise return b
Programmers who are more familiar with the behavior in C might find this
feature surprising, but it allows for a more concise expression of
patterns like null coalescing:
const s = t || "(default)"; // assigns t, or the default value, if t is
null, empty, etc.
Logical assignment
| ??= | Nullish assignment |
|---|---|
| //= | Logical Or assignment |
| &&= | Logical And assignment |
Bitwise
JavaScript supports the following binary bitwise operators:
| & | AND |
|---|---|
| / | OR |
| ^ | XOR |
| ! | NOT |
| << | shift left (zero fill at right) |
| >> | shift right (sign-propagating); copies⦠|
| >>> | shift right (zero fill at left). For po⦠|
Examples:
const x = 11 & 6;
console.log(x); // 2
JavaScript supports the following unary bitwise operator:
Bitwise Assignment
JavaScript supports the following binary assignment operators:
| &= | and |
|---|---|
| /= | or |
| ^= | xor |
| <<= | shift left (zero fill at right) |
| >>= | shift right (sign-propagating); copies⦠|
| >>>= | shift right (zero fill at left). For po⦠|
Examples:
let x=7;
console.log(x); // 7
x<<=3;
console.log(x); // 7->14->28->56
String
| = | assignment |
|---|---|
| + | concatenation |
| += | concatenate and assign |
Examples:
let str = "ab" + "cd"; // "abcd"
str += "e"; // "abcde"
const str2 = "2" + 2; // "22", not "4" or 4.
??
JavaScript's nearest operator is ??, the "nullish coalescing operator",
which was added to the standard in ECMAScript's 11th edition.cite-ref-18[18] In
earlier versions, it could be used via a Babel plugin, and in
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ